import { Plugin } from './Plugin';
import { Cache, CacheOptions } from '../caching/Cache';

export class PluginCache extends Plugin<Plugin.All, { cache: Cache }> {
  constructor(config: Cache | [new (...args: any[]) => Cache, CacheOptions]) {
    super();
    let instance: Cache;

    if (Array.isArray(config)) {
      instance = Cache.initialize(config);
    } else {
      instance = config;
    }

    this.use((ctx, next) => {
      ctx.cache = instance;
      return next();
    });
  }
}
